import { LCrypto } from './utils/lcrypto';
//npm install --save @types/lodash

import { Express, IRouterMatcher, Response, Request, NextFunction } from 'express';
import * as express from 'express';
import { Db } from './utils/db';
import { Http } from './utils/http';

class App {
    private app: Express;
    public hallAddr: string = "";
    private config;//调取配置文件
    constructor() {
        this.app = express();


        this.app.all('*', (req: Request, res: Response, next: NextFunction) => {
            res.header("Access-Control-Allow-Origin", "*");
            res.header("Access-Control-Allow-Headers", "X-Requested-With");
            res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
            res.header("X-Powered-By", 'leiguan server!!')
            res.header("Content-Type", "application/json;charset=utf-8");
            next();
        });
        this.app.get("/", (req: Request, res: Response) => {
        
            res.send('hello world!!');
            console.log("hhhhhhhhhhhhhhhhhh")
        });
        var server = this.app.listen(3000, () => {
            var host = server.address().address;
            var port = server.address().port;
            console.log('Example app listening at http://%s:%s', host, port)
        });

        //
        this.app.get('/guest',(req:Request, res: Response)=>{
            var account = "guest_" + req.query.account;
            var sign = LCrypto.md5(account + req.ip + this.config.ACCOUNT_PRI_KEY);
            var ret = {
                errcode:0,
                errmsg:"ok",
                account:account,
                halladdr:this.hallAddr,
                sign:sign
            }
            Http.sendJson(res,ret);
            console.log(account);
        })
    }
    public start = function(cfg){
        this.config = cfg;
        this.hallAddr = this.config.HALL_IP  + ":" + this.config.HALL_CLIENT_PORT;
        this.app.listen(this.config.CLIENT_PORT);
        console.log("account server is listening on " + this.config.CLIENT_PORT);
    }
}
new App();